# encoding: utf-8

import os
import json
from typing import List


def get_train_datas(data_path: str, text_name: str, label_name: str) -> List[str]:
    if not os.path.exists(data_path):
        raise Exception("file not exists")

    result = []
    with open(data_path, "r", encoding="utf-8") as f:
        data_source = json.load(f)

    for data in data_source:
        text = data.get(text_name)
        try:
            label = data.get(label_name).strip()
        except Exception as e:
            print(e)
            continue
        result.append(f"{text}__{label}\n")

    return result


def to_text(text_path: str, texts: List[str]):
    with open(text_path, "w", encoding="utf-8") as f:
        for text in texts:
            f.write(text)
